//
//  ShellIncomeStatementView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/9.
//  Copyright © 2019年 VV. All rights reserved.
//

#import "ShellIncomeStatementView.h"
#import "ShellIncomeStatementCell.h"
#import "LZBaseTableView.h"
#import "ShellPledgeViewModel.h"

@interface  ShellIncomeStatementView() <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) LZBaseTableView *tableView;

@property (nonatomic,strong) ShellPledgeViewModel *viewModel;

@end

@implementation ShellIncomeStatementView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.cornerRadius = 8;
        self.backgroundColor = COLOR_TINT;
        [self setupView];
    }
    return self;
}

#pragma mark - setupView
- (void)setupView{
    
    UILabel *titleLab = [UILabel lz_labelWithText:LZLocalizedString(@"收益说明") fontSize:k_textBoldFont(14) color:UIColorHex(0xFFFFFF)];
    [titleLab lz_centerAlignment];
    [self addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(22);
        make.right.mas_equalTo(-22);
        make.height.mas_equalTo(22);
    }];
    
    ///!!!:取消
    UIButton *cancelBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"取消") titleColor:UIColorHex(0x8D99B2) fontSize:17];
    cancelBtn.backgroundColor = UIColorHex(0x0D3354);
    cancelBtn.cornerRadius = 6;
    [self addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.bottom.mas_equalTo(-20);
        make.width.mas_equalTo((K_SCREENWIDTH - 150)/2.0);
        make.height.mas_equalTo(40);
    }];
    
    @weakify(self);
    [[cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [ShellIncomeStatementView dismiss];
    }];

    
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(55);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-90);
    }];
    
    UIButton *sureBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"确定") titleColor:UIColorHex(0xFFFFFF) fontSize:17];
    [self addSubview:sureBtn];
    sureBtn.cornerRadius = 8;
    [sureBtn setGradientBackgroundWithColors:@[UIColorHex(0x65DFFE),UIColorHex(0x2C6FE)] locations:@[@(0), @(1.0f)] startPoint:CGPointMake(1, 0.5) endPoint:CGPointMake(0, 0.5)];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(-20);
        make.width.mas_equalTo((K_SCREENWIDTH - 150)/2.0);
        make.height.mas_equalTo(40);
    }];
    
    [[sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [ShellIncomeStatementView dismiss];
    }];
    
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.tableView reloadData];
    }];
    
    //发送请求
    [self.viewModel getProfitInfo];
}



#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShellIncomeStatementCell *statementCell = [tableView dequeueReusableCellWithIdentifier:@"ShellIncomeStatementCell"];
    statementCell.modelObject = self.viewModel.dataArray[indexPath.row];
    return statementCell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, K_SCREENWIDTH - 100, 30)];
    sectionView.backgroundColor = COLOR_TINT;
    
    //信用分
    UILabel *creditLab = [UILabel lz_labelWithText:LZLocalizedString(@"信用分") fontSize:k_textFont(12) color:UIColorHex(0x8D99B2)];
    creditLab.numberOfLines = 0;
    creditLab.frame = CGRectMake(0, 0, 50, 30);
    creditLab.adjustsFontSizeToFitWidth = YES;
    [sectionView addSubview:creditLab];
       
    //可质押(USDT)
    UILabel *pledgeLabel = [UILabel lz_labelWithText:LZLocalizedString(@"可质押(USDT)") fontSize:k_textFont(12) color:UIColorHex(0x8D99B2)];
    pledgeLabel.numberOfLines = 0;
    pledgeLabel.adjustsFontSizeToFitWidth = YES;
    pledgeLabel.frame = CGRectMake(60, 0, 80, 30);
    [sectionView addSubview:pledgeLabel];
       
    //订单
    UILabel *orderLab = [UILabel lz_labelWithText:LZLocalizedString(@"订单") fontSize:k_textFont(12) color:UIColorHex(0x8D99B2)];
    orderLab.frame = CGRectMake(145, 0, 70, 30);
    [sectionView addSubview:orderLab];
    
    //收益
    UILabel *incomeLab = [UILabel lz_labelWithText:LZLocalizedString(@"收益") fontSize:k_textFont(12) color:UIColorHex(0x8D99B2)];
    [incomeLab lz_centerAlignment];
    incomeLab.numberOfLines = 0;
    incomeLab.adjustsFontSizeToFitWidth = YES;
    incomeLab.frame = CGRectMake(220, 0, K_SCREENWIDTH - 126 - 220, 30);
    [sectionView addSubview:incomeLab];

    return sectionView;
}

#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_TINT;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[ShellIncomeStatementCell class] forCellReuseIdentifier:@"ShellIncomeStatementCell"];
        _tableView.rowHeight = 30;
    }
    return _tableView;
}

- (ShellPledgeViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[ShellPledgeViewModel alloc] init];
    }
    return _viewModel;
}

+ (void)show{
    
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    for (UIView * aView in window.subviews) {
        if (aView.tag  == 20191003){
            return;
        }
    }
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, K_SCREENWIDTH, K_SCREENHEIGHT)];
    contentView.tag = 20191003;
    contentView.backgroundColor = [UIColorHex(0x000000) colorWithAlphaComponent:0.5];
    [window addSubview:contentView];
    ShellIncomeStatementView *statementView = [[ShellIncomeStatementView alloc]initWithFrame:CGRectMake(35, (K_SCREENHEIGHT - 380)/2.0, K_SCREENWIDTH - 70, 380)];
    [contentView addSubview:statementView];
    
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
    [contentView addGestureRecognizer:tapGestureRecognizer];
       
    [[tapGestureRecognizer rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
       [ShellIncomeStatementView dismiss];
    }];
    
}

+ (void)dismiss{
    
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    for (UIView * aView in window.subviews) {
        if (aView.tag  == 20191003){
            [aView removeFromSuperview];
        }
    }
}


@end
